package com.jrtc.backend.controller;

import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.RoleMenu;
import com.jrtc.service.RoleMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 角色与菜单关联表(RoleMenu)表控制层
 *
 * @author makejava
 * @since 2022-09-06 09:51:22
 */
@RestController
@RequestMapping("roleMenu")
public class RoleMenuController {
    /**
     * 服务对象
     */
    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RoleMenu selectOne(Integer id) {
        return this.roleMenuService.queryById(id);
    }
    
//    @RequestMapping("getRoleMenuList")
//    public ResultDTO getRoleMenuList(@RequestBody RoleMenu roleMenu)
//    {
//        IPage<RoleMenu> ipage = roleMenuService.queryRoleMenuList(roleMenu);
//        return ResultDTOBuilder.success(ipage);
//    }

    @RequestMapping("insertRoleMenu")
    public ResultDTO insertRoleMenu(@RequestBody RoleMenu roleMenu)
    {
//        roleMenu.setId(UUID.randomUUID().toString());
        RoleMenu tmpRoleMenu = roleMenuService.insert(roleMenu);
        if (tmpRoleMenu != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getRoleMenuById")
    public ResultDTO getRoleMenuById(@RequestBody Map<String, String> map)
    {
        String roleMenuId = map.get("roleMenuId");
        RoleMenu roleMenu = roleMenuService.queryById(Integer.parseInt(roleMenuId));
        if (Strings.isNullOrEmpty(roleMenuId) || roleMenu==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(roleMenu);
    }

    @RequestMapping("updateRoleMenu")
    public ResultDTO updateRoleMenu(@RequestBody RoleMenu roleMenu)
    {
        if (roleMenu==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (roleMenuService.update(roleMenu)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteRoleMenu")
    public ResultDTO deleteRoleMenu(@RequestBody Map<String,String> map)
    {
        String roleMenuId = map.get("roleMenuId");
        if (!Strings.isNullOrEmpty(roleMenuId))
        {
            if (roleMenuService.deleteById(Integer.parseInt(roleMenuId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }
}
